<?php

include_once("connexion.php");

$_SESSION['url'] = 'confirmation.php?id='.$_SESSION["userId"];

if (!isset($_SESSION["userId"])){
	header("location:authentification.php");
}
	
	$id = $_POST['id_prix'];	
	
	echo $_POST['debut'];
	if (($_POST['debut'] < date("Y-m-d")) || ($_POST['debut'] > $_POST['fin']) ){
		header("location:detail_location.php?id=".$_SESSION['id_traitement']);
		$_SESSION['correcte'] = true;
	}

	
	/*$query = "SELECT location.debut_location, location.fin_location
			  FROM location
			  WHERE id_logement = '".$id."'";
			  
	$resultat = mysqli_query($connexion, $query);
	
		while ($date = mysqli_fetch_array($resultat)) {
		
			if (
			
			{
				
			header("location:detail_location.php?id=".$_SESSION['id_traitement']);
			$_SESSION['correcte'] = true;				
			}

		}*/
	
	
	
	$id = $_POST['id_prix'];	
	
	$debut_location = date("d-m-Y", strtotime($_POST['debut']));
	$fin_location = date("d-m-Y", strtotime($_POST['fin']));
	$query = "SELECT logement.tarif_journee, logement.piece, logement.surface, ville.ville, type.type, pays.pays 
			  FROM logement, ville, type, pays
			  WHERE ville.id = logement.id_ville AND pays.id = ville.id_pays AND type.id = logement.id_type AND logement.id = '".$id."'";
	$resultat = mysqli_query($connexion, $query);

	
	$temps = ((strtotime($fin_location) - strtotime($debut_location)) / 86400);
		
		$conf = mysqli_fetch_array($resultat);{
		$tarif =  $conf['tarif_journee'] * $temps;
		$type = $conf['type'];
		$piece = $conf['piece'];
		$surface = $conf['surface'];
		$ville = $conf['ville'];
		$pays = $conf['pays'];
	}
	
	if(isset($_POST['valider'])){
		$debut_location = date("Y-m-d", strtotime($_POST['debut']));
		$fin_location = date("Y-m-d", strtotime($_POST['fin']));
			
			$query = "INSERT INTO location VALUES ('', '".$_POST['id_logement']."', '".$_SESSION['userId']."', '".$debut_location."', '".$fin_location."', '".$_POST['prix']."')";
			echo $query;
			$insert = mysqli_query($connexion, $query);
			header("location:reservation.php");
	}




include_once("banniere.html");
include_once("confirmation.html");
include_once("footer.html");

?>